package com.zc.study.java爬虫.eastMoney;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class AnalysisParentForEast implements AnalysisParent {
    @Override
    public void getAllChildIndex(String url) {
        List<String> getChildInfoList = new ArrayList<>();
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
            System.out.println("开始查找页面" + url);
            Elements links = doc.select("a[href]");
            String articleReg = "http://finance.eastmoney.com/a/([0-9]*).html";
            String indexReg = "http://finance.eastmoney.com/a/";
            for (Element link : links) {
                String childUrl = link.attr("abs:href");
                System.out.println("查找父页面为::" + childUrl);
                if (EastCash.STORE_MAP.get(childUrl) == null) {
                    if (childUrl.matches(articleReg)) {
                        EastCash.STORE_MAP.put(childUrl, new BaseStore());
                        //new AnalysisEast().analysis(childUrl, EastCash.STORE_MAP);
                    } else {
                        if (childUrl.contains(indexReg)) {
                            boolean add = EastCash.PARENT_INDEX.add(childUrl);
                            if (add) {
                                getChildInfoList.add(childUrl);
                            }
                        }
                    }
                }
            }
            //寻找其他子页面article页面的url链接
            getChildInfoList.forEach(this::getAllChildIndex);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void storeFile() {
        new AnalysisEast().analysis(EastCash.STORE_MAP);
    }
}
